package org.study.module.zookeeper;

import org.study.entity.ClusterInfoEntity;
import org.study.entity.ClusterModuleInfoEntity;
import org.study.module.common.ModuleCommon;
import org.study.module.common.ModuleConstant;
import org.study.shell.ShellBase;
import org.study.shell.ShellFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ZookeeperShell {

    public static List<String> install(String softPath, String installPath,String id) {
        List<String> commands = new ArrayList<String>();
        installPath = installPath + "/"+ ModuleConstant.ZOOKEEPER;
        commands.addAll(ShellFile.createPath(installPath + "/data"));
        commands.addAll(ShellFile.createPath(installPath + "/logs"));

        commands.add("sudo chmod -R 755 " + installPath);
        commands.add("echo \"查看"+ModuleConstant.ZOOKEEPER+"的安装包\"");
        commands.addAll(ShellFile.findByName(softPath, ModuleConstant.ZOOKEEPER, ModuleConstant.ZOOKEEPER+"_tar"));
        commands.add("tar -xzvf $"+ModuleConstant.ZOOKEEPER+"_tar -C " + installPath);
        commands.add("cd " + installPath);
        commands.addAll(ShellFile.findByName(installPath, ModuleConstant.ZOOKEEPER, ModuleConstant.ZOOKEEPER+"_install"));
        List<String> pros = new ArrayList<>();
        pros.add("export ZOOKEEPER_HOME=$hadoop_install");
        pros.add("export PATH=\\$PATH:\\$ZOOKEEPER_HOME/bin");
        commands.addAll(ShellBase.addProfile(pros, ModuleConstant.ZOOKEEPER+"_env.sh"));
        commands.add("echo \"设置Zookeeper的myid\"");
        commands.add("echo \""+id+"\" > "+installPath+"/data/myid");
        commands.addAll(ShellBase.firewallAddPorts("2181,2888,3888"));
        return commands;
    }

    public static List<String> install(ClusterInfoEntity cluster, ClusterModuleInfoEntity entity,Integer id) {
        List<String> commands = new ArrayList<>();
        String installPath = cluster.getInstallPath() + "/"+ ModuleConstant.ZOOKEEPER;
        commands.addAll(ShellFile.createPath(installPath + "/data"));
        commands.addAll(ShellFile.createPath(installPath + "/logs"));
        commands.add("sudo chmod -R 755 " + installPath);
        commands.addAll(ModuleCommon.installByTar(cluster,entity));
        List<String> pros = new ArrayList<>();
        pros.add("export ZOOKEEPER_HOME=$hadoop_install");
        pros.add("export PATH=\\$PATH:\\$ZOOKEEPER_HOME/bin");
        commands.addAll(ShellBase.addProfile(pros, ModuleConstant.ZOOKEEPER+"_env.sh"));
        commands.add("echo \"设置Zookeeper的myid\"");
        commands.add("echo \""+id+"\" > "+installPath+"/data/myid");
        commands.addAll(ShellBase.firewallAddPorts("2181,2888,3888"));
        return commands;
    }

    public static List<String> config(String installPath, Map<Integer,String> idHost){
        List<String> commands = new ArrayList<>();
        commands.add("cat > $ZOOKEEPER_HOME/conf/zoo.cfg << EOF");
        commands.add("tickTime=2000");
        commands.add("initLimit=10");
        commands.add("syncLimit=5");
        commands.add("dataDir="+installPath+"/zookeeper/data");
        commands.add("dataLogDir="+installPath+"/zookeeper/logs");
        commands.add("clientPort=2181");
        //server.[myid]=[IP]:[Follower与Leader通信端口]:[选举端口]
        //commands.add("server.1=nn1:2888:3888");
        for(Integer id : idHost.keySet()){
            commands.add("server."+id+"="+idHost.get(id)+":2888:3888");
        }
        commands.add("# 开启ACL认证（安全必要）");
        commands.add("authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
        commands.add("requireClientAuthScheme=sasl");
        commands.add("EOF");
        return commands;
    }

    /**
     * 启动服务
     */
    public static String start(){
        return "sh $ZOOKEEPER_HOME/bin/zkServer.sh start -daemon  # 以守护进程方式运行";
    }

    /**
     * 停止服务
     */
    public static String stop(){
        return "sh $ZOOKEEPER_HOME/bin/zkServer.sh stop -daemon  # 以守护进程方式运行";
    }

    /**
     * 重启服务
     */
    public static String restart(){
        return "sh $ZOOKEEPER_HOME/bin/zkServer.sh stop -daemon  # 以守护进程方式运行";
    }

}
